package CMD;

import java.awt.image.ImagingOpException;
import java.io.*;
import java.lang.reflect.Array;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.regex.Pattern;

public class Instruction {
    //获取当前的路径
    public static String getPath(ArrayList<String> p){
        String path = "";
        for(String s : p){
            path += s;
        }
        return path;
    }
    //打印当前路径
    public static void showPath(ArrayList<String> p){
        String path = getPath(p);
        System.out.print(path);
        if(p.size() == 1) System.out.print("\\");
        System.out.print(">");
    }
    //CD操作指令
    public static void cdOperation(String nextPath,ArrayList<String> p){
        if(nextPath.equals("..")){
            if(p.size() > 1)p.remove(p.size() - 1);
            showPath(p);
        }
        //退回到上一级目录
        else {
            if (new File(getPath(p) + "\\" + nextPath).exists()) {
                nextPath = "\\" + nextPath;
                p.add(nextPath);
                showPath(p);
            }
            //进入到新路径(相对路径)
            else {
                if(new File(nextPath).exists()){
                    p.clear();
                    String regex = "[\\\\:]+";
                    String eachPath[] = nextPath.split(regex);
                    p.add(eachPath[0] + ":");
                    for(int i = 1; i < eachPath.length; i++){
                        p.add("\\" + eachPath[i]);
                    }
                    showPath(p);
                }
                //绝对路径
                else {
                    System.out.println("系统找不到指定的路径。");
                    showPath(p);
                }
                //路径错误
            }
        }
    }
    //Touch操作指令
    public static void touchOperation(String fileName, ArrayList<String> p){
        File file = new File(Instruction.getPath(p) + "\\" + fileName);
        if(!file.exists()){
            try {
                file.createNewFile();
                System.out.println("文件创建成功！");
            }
            catch (IOException e){
                System.out.println("发生错误");
            }
        }
        //文件不存在，进行创建
        else{
            System.out.println("子目录或文件 " + fileName + " 已经存在。");
        }
        //文件存在
        showPath(p);
    }
    //Delete操作指令
    public static void deleteOperation(String fileName,ArrayList<String> p){
        File file = new File(Instruction.getPath(p) + "\\" + fileName);
        if(!file.exists()){
            System.out.println("找不到 " + file.getAbsolutePath());
        }
        //文件不存在
        else{
            file.delete();
            System.out.println("文件删除成功！");
        }
        //文件存在，删除文件
        showPath(p);
    }
    //Mkdir操作指令
    public static void mkdirOperation(String dirName,ArrayList<String> p){
        File file = new File(Instruction.getPath(p) + "\\" + dirName);
        if(file.mkdir()){
            System.out.println("文件夹创建成功！");
        }
        //进行文件夹的创建
        else{
            System.out.println("子目录或文件 " + dirName +" 已经存在。");
        }
        //要创建的文件夹已经存在
        showPath(p);
    }
    //Move操作指令
    public static void moveOperation(String source,ArrayList<String> p){
        char a[] = source.toCharArray();
        int index = 0;
        for(int i = 0; i < a.length; i++) {
            if(a[i] == ' '){
                index = i;
                break;
            }
        }
        String target = source.substring(index + 1,source.length());
        source = source.substring(0,index);
        File sources = new File(getPath(p) + "\\" + source);
        File targets = new File(getPath(p) + "\\" + target);
            if(!sources.exists()){
                System.out.println("系统找不到指定的文件。");
            }
            else if(targets.exists()){
                try {
                    Files.move(sources.toPath(), targets.toPath().resolve(sources.getName()));
                }
                catch (IOException e){
                    System.out.println("文件已存在。");
                }
            }
            else if(new File(target).exists()){
                targets = new File(target);
                try {
                    Files.move(sources.toPath(),targets.toPath().resolve(sources.getName()));
                }
                catch (IOException e){
                    System.out.println("文件已存在");
                }
            }
            else{
                try {
                    Files.move(sources.toPath(),sources.toPath().resolveSibling(target));
                }
                catch (IOException e){
                    e.printStackTrace();
                }
            }
            showPath(p);
    }
    //显示当前文件夹下的全部文件
    public static void showAllFiles(ArrayList<String> p){
        String allFiles[] = new File(getPath(p)).list();
        for (String s : allFiles){
            System.out.println(s);
        }
        showPath(p);
    }
    //查看文件内容
    public static void readFile(String fileName,ArrayList<String> p){
        File file = new File(getPath(p) + "\\" + fileName);
        if(!file.canRead()){
            System.out.println("文件不可读");
        }
        else{
            try {
                char a[] = new char[4];
                FileReader in = new FileReader(file);
                int n;
                while((n = in.read(a)) != -1){
                    String s = new String(a,0,n);
                    System.out.print(s);
                }
                System.out.println();
                in.close();
            }
            catch (IOException e){
                System.out.println("读入异常");
            }
        }
        showPath(p);
    }
    //写文件
    public static void writeFile(String fileName,ArrayList<String> p){
        System.out.println("是否追加? (Y/N)");
        Scanner sc = new Scanner(System.in);
        try {
            String choose = sc.nextLine();
            if (choose.equalsIgnoreCase("Y")) {
                FileWriter out = new FileWriter(getPath(p) + "\\" + fileName,true);
                String content = sc.nextLine();
                out.write(content.toCharArray(),0,content.toCharArray().length);
                out.flush();
                out.close();
                showPath(p);
            }
            else if(choose.equalsIgnoreCase("N")){
                FileWriter out = new FileWriter(getPath(p) + "\\" + fileName);
                String content = sc.nextLine();
                out.write(content.toCharArray(),0,content.toCharArray().length);
                out.flush();
                out.close();
                showPath(p);
            }
            else{
                System.out.println("输入错误");
                writeFile(fileName,p);
            }
        }
        catch (IOException e){

        }
    }
}
